package minov.test;

import java.awt.color.CMMException;
import java.util.Arrays;

import minov.parser.ArgumentDefinitionException;
import minov.parser.ArgumentParser;
import minov.parser.CommandArgument;
import minov.parser.IntegerArgument;
import minov.parser.SwitchArgument;
import minov.parser.ValidationResult;

public class CommandsParsing {
   public CommandsParsing() {
      initialize();
   }

   ArgumentParser parser = new ArgumentParser();
   CommandArgument commit = new CommandArgument("commit","ci");
   CommandArgument update = new CommandArgument("update");
   CommandArgument checkout = new CommandArgument("checkout","co");

   private void initialize() {
      
      parser.addArgument("commit", commit);
      parser.addArgument("update", update);
      parser.addArgument("checkout", checkout);
   }

   private void print() {
      
      if (commit.isEntered()) {
         System.out.println("Commit arguments: "+ Arrays.toString(commit.getArguments()));
      }
      if (update.isEntered()) {
         System.out.println("Update arguments: "+ Arrays.toString(update.getArguments()));
      }
      if (checkout.isEntered()) {
         System.out.println("Checkout arguments: "+ Arrays.toString(checkout.getArguments()));
      }
   }

   public void parseArguments(String[] arguments) {

      printTestsSeparator(arguments);

      // result of parsing, if all arguments are valid
      ValidationResult result = null;
      try {

         result = parser.parseArguments(arguments);

         if (result.isValid() == false) {
            System.out.println(result.errorMessage());
         }
         print();

      } catch (ArgumentDefinitionException exception) {
         System.out.println("Exception: " + exception.getMessage());
         return;
      }

   }

   private static void printTestsSeparator(String[] arguments) {
      System.out.println("=========================");
      System.out.println("Parsing arguments: " + Arrays.toString(arguments));
   }

   public static void main(String[] arguments) {

      CommandsParsing resuser = new CommandsParsing();

      String[] simpleArguments = {};
      resuser.parseArguments(simpleArguments);

      String[] simpleArguments2 = { "checkout" };
      resuser.parseArguments(simpleArguments2);

      String[] simpleArguments3 = { "co" ,"username"};
      resuser.parseArguments(simpleArguments3);

      String[] simpleArguments4 = { "update" ,"--recursive","." };
      resuser.parseArguments(simpleArguments4);

      String[] simpleArguments5 = { "commit" ,"--username=smith","."  };
      resuser.parseArguments(simpleArguments5);

      String[] simpleArguments6 = { "ci" ,"--username=smith","."  };
      resuser.parseArguments(simpleArguments6);
   }
}
